﻿
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data;
using Utils;
using PanasonicWarranty.DAL;
namespace PanasonicWarranty.Business
{
    public partial class SysRole
    {
    	#region public properties
		[Required(ErrorMessage="RoleID Required")]
		public int RoleID { get; set; }
		[Required(ErrorMessage="Role Code Required")]
		public string RoleCode { get; set; }
		[Required(ErrorMessage="Role Name Required")]
		public string RoleName { get; set; }
		public string Description { get; set; }
		public int? Ord { get; set; }
		public bool? Disabled { get; set; }
		#endregion

		#region constructor
		public SysRole(){ }

		public SysRole(IDataReader rd)
		{
			this.RoleID = (int)rd["RoleID"];
			this.RoleCode = rd["RoleCode"].ToString();
			this.RoleName = rd["RoleName"].ToString();
			this.Description = rd["Description"].ToString();
			this.Ord = rd["Ord"].Equals(DBNull.Value) ? (int?)null : (int)rd["Ord"];
			this.Disabled = rd["Disabled"].Equals(DBNull.Value) ? (bool?)null : (bool)rd["Disabled"];
		}
		#endregion

		#region private method
        private bool Create()
        {
            return DBSysRole.Create(RoleCode, RoleName, Description, Ord, Disabled);
        }

        private bool Update()
        {
            return DBSysRole.Update(RoleID, RoleCode, RoleName, Description, Ord, Disabled);
        }
		#endregion

		#region public method
		public bool Save()
		{
            if(RoleID != 0)
            {
                return Update();
            }
            else
            {
                return Create();
            }
		}
		#endregion

		#region static method
        public static bool Delete(int RoleID)
        {
            return DBSysRole.Delete(RoleID);
        }

        public static List<SysRole> GetAll()
        {
            return ObjectClass.CreateList<SysRole>(DBSysRole.GetAll());
        }

        public static SysRole GetByKey(int RoleID)
        {
            return ObjectClass.CreateInstance<SysRole>(DBSysRole.GetByKey(RoleID));
        }

        public static List<SysRole> GetByTop(string top, string where, string order)
        {
            return ObjectClass.CreateList<SysRole>(DBSysRole.GetByTop(top, where, order));
        }
		#endregion
	}
}
